package com.yanfei.zero.domain.user.model.perms;

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.yanfei.zero.domain.user.model.BaseModel;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Transient;
import lombok.Data;

/**
 * CREATE TABLE resource (
    resource_id INT AUTO_INCREMENT PRIMARY KEY, -- 资源ID
    resource_name VARCHAR(100) NOT NULL, -- 资源名称
    resource_type VARCHAR(50) NOT NULL, -- 资源类型
    resource_path VARCHAR(255), -- 资源路径
    parent_resource_id INT, -- 父级资源ID
    create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 创建时间
    update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, -- 更新时间
    creator VARCHAR(100), -- 创建人
    updater VARCHAR(100), -- 更新人
    is_available TINYINT(1) DEFAULT 1 -- 是否可用
);
 **/
@Entity
@Table(name="cm_resource")
@Schema(name = "资源")
@Data
public class CmResource extends BaseModel{
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	@Schema(name = "类型 0 菜单  1 功能  2 数据")
	private int type;
	@Schema(name = "路径")
	private String path;
	@Schema(name = "名称")
	private String name;
	@Schema(name = "权限标识")
	private String perms; // system:role:edit
	@Schema(name = "父级ID")
	@Column(name = "parent_id")
	private long parentId;
	@Schema(name = "排序")
	private int sort;
	
	@Transient
	private CmResource parent;
	@Transient
	private List<CmResource> children = new ArrayList<>(); ;
	public void addChild(CmResource node) {
		children.add(node);
//		node.setParent(this);
	}
	
//	INSERT INTO CM_RESOURCE(id, type, path, name, perms, parent_id, sort)     
//	SELECT COALESCE(MAX(id), 0) + 1,   
//	      1,   
//	      NULL,  
//	      10001,   
//	      'system:role:edit',  
//	      COALESCE(MAX(id), 0),   
//	      0   
//	FROM CM_RESOURCE;
}
